﻿using Newtonsoft.Json;
using System.Reflection;

namespace Leo.Common.Json
{
    internal class TypePropertyDescriptor
    {
        private readonly PropertyInfo property;
        public TypePropertyDescriptor(PropertyInfo property)
        {
            this.property = property;
            this.Name = property.Name;
            JsonPropertyAttribute customAttribute = CustomAttributeExtensions.GetCustomAttribute<JsonPropertyAttribute>(property);
            this.JsonName = ((customAttribute != null) ? customAttribute.PropertyName : null);
        }

        public string Name { get; }

        public string JsonName { get; set; }

        public bool IsProperty(string name)
        {
            return name.Equals(this.Name, StringComparison.OrdinalIgnoreCase) || name.Equals(this.JsonName, StringComparison.OrdinalIgnoreCase);
        }
    }
}
